<?php

//http://patft.uspto.gov/netahtml/PTO/help/stopword.htm
$GLOBALS['stopwords'] = array(
	'a', 'has', 'such','have',
	'according', 'having', 'than',
	'all', 'herein', 'that',
	'also', 'however', 'the',
	'an', 'if', 'their',
	'and', 'in', 'then',
	'another', 'into', 'there',
	'are', 'thereby',
	'as', 'is', 'therefore',
	'at', 'it', 'thereof',
	'be', 'its', 'thereto',
	'because', 'means', 'these',
	'been', 'not', 'they',
	'being', 'now', 'this',
	'by', 'of', 'those',
	'claim', 'on', 'thus',
	'onto', 'to',
	'corresponding', 'or', 'use',
	'could', 'other', 'various',
	'described', 'particularly', 'was',
	'desired', 'preferably', 'were',
	'do', 'preferred', 'what',
	'does', 'present', 'when',
	'each', 'provide', 'where',
	'embodiment', 'provided', 'whereby',
	'fig', 'provides', 'wherein',
	'figs', 'relatively', 'which',
	'for', 'respectively', 'while',
	'from', 'said', 'who',
	'further', 'should', 'will',
	'generally', 'since', 'with',
	'had', 'some', 'would',
);

$GLOBALS['plural'] = array(
	array( '/(quiz)$/i',               "$1zes"   ),
	array( '/^(ox)$/i',                "$1en"    ),
	array( '/([m|l])ouse$/i',          "$1ice"   ),
	array( '/(matr|vert|ind)ix|ex$/i', "$1ices"  ),
	array( '/(x|ch|ss|sh)$/i',         "$1es"    ),
	array( '/([^aeiouy]|qu)y$/i',      "$1ies"   ),
	array( '/([^aeiouy]|qu)ies$/i',    "$1y"     ),
	array( '/(hive)$/i',               "$1s"     ),
	array( '/(?:([^f])fe|([lr])f)$/i', "$1$2ves" ),
	array( '/sis$/i',                  "ses"     ),
	array( '/([ti])um$/i',             "$1a"     ),
	array( '/(buffal|tomat)o$/i',      "$1oes"   ),
	array( '/(bu)s$/i',                "$1ses"   ),
	array( '/(alias|status)$/i',       "$1es"    ),
	array( '/(octop|vir)us$/i',        "$1i"     ),
	array( '/(ax|test)is$/i',          "$1es"    ),
	array( '/s$/i',                    "s"       ),
	array( '/$/',                      "s"       )
);

$GLOBALS['singular'] = array(
	'/(quiz)zes$/i'             => "$1",
	'/(matr)ices$/i'            => "$1ix",
	'/(vert|ind)ices$/i'        => "$1ex",
	'/^(ox)en$/i'               => "$1",
	'/(alias)es$/i'             => "$1",
	'/(octop|vir)i$/i'          => "$1us",
	'/(cris|ax|test)es$/i'      => "$1is",
	'/(shoe)s$/i'               => "$1",
	'/(o)es$/i'                 => "$1",
	'/(bus)es$/i'               => "$1",
	'/([m|l])ice$/i'            => "$1ouse",
	'/(x|ch|ss|sh)es$/i'        => "$1",
	'/(m)ovies$/i'              => "$1ovie",
	'/(s)eries$/i'              => "$1eries",
	'/([^aeiouy]|qu)ies$/i'     => "$1y",
	'/([lr])ves$/i'             => "$1f",
	'/(tive)s$/i'               => "$1",
	'/(hive)s$/i'               => "$1",
	'/(li|wi|kni)ves$/i'        => "$1fe",
	'/(shea|loa|lea|thie)ves$/i'=> "$1f",
	'/(^analy)ses$/i'           => "$1sis",
	'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i'  => "$1$2sis",
	'/([ti])a$/i'               => "$1um",
	'/(n)ews$/i'                => "$1ews",
	'/(h|bl)ouses$/i'           => "$1ouse",
	'/(corpse)s$/i'             => "$1",
	'/(us)es$/i'                => "$1",
	'/s$/i'                     => ""
);

$GLOBALS['irregular'] = array(
	'move'   => 'moves',
	'foot'   => 'feet',
	'goose'  => 'geese',
	'sex'    => 'sexes',
	'child'  => 'children',
	'man'    => 'men',
	'tooth'  => 'teeth',
	'person' => 'people'
);

$GLOBALS['uncountable'] = array(
	'sheep',
	'fish',
	'deer',
	'series',
	'species',
	'money',
	'rice',
	'information',
	'equipment'
);

//plural -> singular
function singularize( $string ) {
	global $uncountable, $irregular, $singular;
	// save some time in the case that singular and plural are the same
	if ( in_array( strtolower( $string ), $uncountable ) )
		return $string;

	// check for irregular plural forms
	foreach ( $irregular as $result => $pattern )
	{
		$pattern = '/' . $pattern . '$/i';

		if ( preg_match( $pattern, $string ) )
			return preg_replace( $pattern, $result, $string);
	}

	// check for matches using regular expressions
	foreach ( $singular as $pattern => $result )
	{
		if ( preg_match( $pattern, $string ) )
			return preg_replace( $pattern, $result, $string );
	}

	return $string;
}

//singular -> plural
function pluralize( $string ) {
  global $uncountable, $irregular, $plural;
	// save some time in the case that singular and plural are the same
  if ( in_array( strtolower( $string ), $uncountable ) ) {
	return $string;
 }
        // check for irregular singular forms
  foreach ( $irregular as $noun ) {
	if ( strtolower( $string ) == $noun[0] ) {
	   return $noun[1];
	}
  }
 
	// check for matches using regular expressions
  foreach ( $plural as $pattern ) {
	if ( preg_match( $pattern[0], $string ) ) {
	  return preg_replace( $pattern[0], $pattern[1], $string );
	}
  }

  return $string;
}


function process_keywords($string) {
	global $stopwords;
	$min = C('config_search_min_word_length');
	$max = C('config_search_max_word_length');
	$stopword = C('config_search_stopword');
	
	$keywords = array();
	$words = explode(' ', trim(preg_replace('/\s+/', ' ', $string)));
	$words = array_unique($words);
	foreach ($words as $word) {
		if (strlen($word) < $min || strlen($word) > $max) continue;
		if ($stopword && in_array($word, $stopwords)) continue;
		
		$pluralize = pluralize($word);
		$singularize = singularize($word);
		
		if ($pluralize !== $word && strpos($pluralize, $word) === FALSE) {
			$keywords[] = array($pluralize, $word);
		}
		elseif ($singularize !== $word && strpos($word, $singularize) === FALSE) {
			$keywords[] = array($singularize, $word);
		}
		else $keywords[] = $word;
	}
	return $keywords;
}